//
//  ForgetViewController.m
//  GetFit3.0
//
//  Created by 黄建华 on 2017/5/25.
//  Copyright © 2017年 lxc. All rights reserved.
//

#import "ForgetViewController.h"

#define CODECOUNTVALUE                       60
#import <MOBFoundation/MOBFoundation.h>
#import "YJLocalCountryData.h"
#define TRY_AGAIN_GETSMSCODE_ALERTVIEW_TAG   10
#define CLICKLEFTBUTTON_ALERTVIEW_TAG        11
#define COMMITCODE_SUCCES_ALERTVIEW_TAG      12
#define TRY_AGAIN_GETVOICECODE_ALERTVIEW_TAG 13
#import "AFNetworking.h"

@interface ForgetViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel   *naTitle;
@property (nonatomic, strong) UIButton  *loginButton;
@property (nonatomic, strong) UIButton  *codeButton;
@property (nonatomic, assign) NSInteger codeCount;
@property (nonatomic, assign) BOOL      isCodeSuccessFul;
@property (nonatomic, assign) BOOL      showInfo;
@property (nonatomic, strong) UIView    *passViewSecret; // 密保页面
@property (nonatomic, strong) UIView    *passViewCode;;
@property (nonatomic, assign) NSInteger passWordType;
@property (nonatomic, strong) NSArray   *passWordArray;
@property (nonatomic, strong) UILabel   *findLabelType;

// 密保
@property (nonatomic, strong) UILabel     *question1;
@property (nonatomic, strong) UILabel     *question2;
@property (nonatomic, strong) UITextField *answer1;
@property (nonatomic, strong) UITextField *answer2;
@property (nonatomic, strong) UITextField *passWord1;
@property (nonatomic, strong) UITextField *passWord2;
@property (nonatomic, strong) NSString    *que1;
@property (nonatomic, strong) NSString    *que2;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SNCOLORWHITE;
    
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20 + SNIPXTOPHEIGHT);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = SNFRAME(0, 20 + SNIPXTOPHEIGHT, kSCREEN_WIDTH, 44);
    naView.backgroundColor = WELLGOCOLORWHITE;
    [self.view addSubview:naView];
    naView.layer.shadowColor   = WELLGOCOLORBLACK.CGColor;//shadowColor阴影颜色
    naView.layer.shadowOffset  = CGSizeMake(1, 1);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    naView.layer.shadowOpacity = 0.5;//阴影透明度，默认0
    naView.layer.shadowRadius  = 1;//阴影半径，默认3
    
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = CGRectMake(5, 20 + SNIPXTOPHEIGHT, 44, 44);
    image.image = [UIImage image:@"进入"];
    [self.view addSubview:image];
    
    UIButton *backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(0, 20+FitSimpleIpx(0, 20), 70, 44);
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    _naTitle               = [[UILabel alloc] initWithFrame:SNFRAME(0, 0, HHHWIDTH, 20)];
    _naTitle.center        = CGPointMake(HHHWIDTH/2, backButton.center.y);
    _naTitle.text          = SN_Text(@"忘记密码?");
    _naTitle.font          = SNSFONT(17.0);
    _naTitle.textAlignment = NSTextAlignmentCenter;
    _naTitle.textColor     = WELLGOCOLORBLACK;
    [self.view addSubview:_naTitle];
    
    UIView *grayView = [[UIView alloc] init];
    grayView.frame           = SNFRAME(0, naView.bottom, screenWidth, 60);
    grayView.backgroundColor = [UIColor colorFromHex:0xefefef];
    [self.view addSubview:grayView];
    
    UILabel *findLabel = [[UILabel alloc] initWithFrame:SNFRAME(10, 10, screenWidth, 20)];
    findLabel.text          = SN_Text(@"找回方式");
    findLabel.font          = SNFONT(14.0);
    findLabel.textAlignment = NSTextAlignmentLeft;
    findLabel.textColor     = SNCOLORBLACK;
    [grayView addSubview:findLabel];
    
    _findLabelType               = [[UILabel alloc] initWithFrame:SNFRAME(10, findLabel.bottom+10, screenWidth-30, 20)];
    _findLabelType.font          = SNFONT(15);
    _findLabelType.textAlignment = NSTextAlignmentLeft;
    _findLabelType.textColor     = SNCOLORBLACK;
    [grayView addSubview:_findLabelType];
    
    UIButton *button02 = [[UIButton alloc] init];
    button02.frame = SNFRAME(grayView.right - 33, _findLabelType.centerY-15, 30, 30);
    [button02 setImage:[UIImage image:@"注册问题下拉"] forState:UIControlStateNormal];
    [grayView addSubview:button02];
    [button02 addTarget:self action:@selector(buttonClickSender:) forControlEvents:UIControlEventTouchUpInside];
    
    BOOL typeValue = [SECREETTYPE getBOOLValue];
    if (typeValue) {
        _passWordArray = [NSArray arrayWithObjects:SN_Text(@"通过密保问题"), SN_Text(@"通过验证码"), nil];
        _passWordType  = 1;
    } else {
        _passWordArray = [NSArray arrayWithObjects:SN_Text(@"通过验证码"), nil];
        _passWordType  = 2;
    }
    _findLabelType.text = [_passWordArray firstObject];
    
    _passViewSecret = [[UIView alloc] initWithFrame:SNFRAME(0, grayView.bottom, screenWidth, HHHHEIGHT - grayView.bottom)];
    [self.view addSubview:_passViewSecret];
    
    _passViewCode = [[UIView alloc] initWithFrame:SNFRAME(0, grayView.bottom, screenWidth, HHHHEIGHT - grayView.bottom)];
    [self.view addSubview:_passViewCode];
    
    [self changeFindPassWord];
    
    // 验证码 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _showInfo         = YES;
    _codeCount        = CODECOUNTVALUE;
    _areaButton       = [[UIButton alloc] init];
    _areaButton.frame = CGRectMake(20, -30, 50, 35);
    UIView *view1 = [[UIView alloc] init];
    view1.frame               = CGRectMake(45, -30, kSCREEN_WIDTH - 90, 35);
    view1.backgroundColor     = WELLGOCOLORWHITE;
    view1.alpha               = 0.07;
    view1.layer.cornerRadius  = 18;
    view1.layer.masksToBounds = YES;
    //    [_passViewCode addSubview:view1];
    
    UIView *view2 = [[UIView alloc] init];
    view2.frame               = CGRectMake(30, view1.y + 35 + 20, 145, 35);
    view2.backgroundColor     = WELLGOCOLORWHITE;
    view2.alpha               = 0.07;
    view2.layer.cornerRadius  = 18;
    view2.layer.masksToBounds = YES;
    [_passViewCode addSubview:view2];
    
    UIView *view3 = [[UIView alloc] init];
    view3.frame               = CGRectMake(30, view2.y + 35 + 20, kSCREEN_WIDTH - 60, 35);
    view3.backgroundColor     = WELLGOCOLORWHITE;
    view3.alpha               = 0.07;
    view3.layer.cornerRadius  = 18;
    view3.layer.masksToBounds = YES;
    [_passViewCode addSubview:view3];
    
    UIView *view4 = [[UIView alloc] init];
    view4.frame               = CGRectMake(30, view3.y + 35 + 20, kSCREEN_WIDTH - 60, 35);
    view4.backgroundColor     = WELLGOCOLORWHITE;
    view4.layer.cornerRadius  = 18;
    view4.alpha               = 0.07;
    view4.layer.masksToBounds = YES;
    [_passViewCode addSubview:view4];
    
    _textFiled4       = [[UITextField alloc] init];
    _textFiled4.frame = CGRectMake(30, _areaButton.origin.y +10 + 165, kSCREEN_WIDTH - 60, 25);
    [_textFiled4 changePlaceholder:SN_Text(@"请再次输入密码") withColor:HHHFONTCOLOR withFont:14.0];
    _textFiled4.textColor = WELLGOCOLORBLACK;
    _textFiled4.delegate  = self;
    [_passViewCode addSubview:_textFiled4];
    _textFiled4.secureTextEntry = YES;
    
    _loginButton                     = [[UIButton alloc] init];
    _loginButton.frame               = CGRectMake(30, _passViewCode.bottom - 200, kSCREEN_WIDTH - 60, 35);
    _loginButton.layer.cornerRadius  = 5;
    _loginButton.layer.masksToBounds = YES;
    _loginButton.backgroundColor     = SNCOLORBLACK;
    [_loginButton setTitleNormal:SN_Text(@"邮箱找回密码")];
    [_loginButton.titleLabel setFont:HHHFONTSIZE];
    [_loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton setTitleColorNormal:SNCOLORWHITE];
    [_loginButton setFontSize:15.0];
    [_passViewCode addSubview:_loginButton];
    
    KK_ShareData.registrationType = 2;
    _textFiled1                   = [[UITextField alloc] init];
    _textFiled1.frame             = CGRectMake(_areaButton.origin.x + 40, _areaButton.origin.y +10, view1.width - 40, 25);
    [_textFiled1 changePlaceholder:SN_Text(@"请输入邮箱") withColor:HHHFONTCOLOR withFont:14.0];
    _textFiled1.textColor = WELLGOCOLORBLACK;
    _textFiled1.delegate  = self;
    //    [_passViewCode addSubview:_textFiled1];
    
    _textFiled2              = [[UITextField alloc] init];
    _textFiled2.keyboardType = UIKeyboardTypeNumberPad;
    _textFiled2.frame        = CGRectMake(30, _areaButton.origin.y +10 + 55, (screenWidth-60)*2/3-15, 25);
    [_textFiled2 changePlaceholder:SN_Text(@"请输入验证码") withColor:HHHFONTCOLOR withFont:14.0];
    _textFiled2.textColor = WELLGOCOLORBLACK;
    _textFiled2.delegate  = self;
    [_passViewCode addSubview:_textFiled2];
    
    _textFiled3       = [[UITextField alloc] init];
    _textFiled3.frame = CGRectMake(30, _areaButton.origin.y +10 + 110, screenWidth-60, 25);
    [_textFiled3 changePlaceholder:SN_Text(@"请输入密码") withColor:HHHFONTCOLOR withFont:14.0];
    _textFiled3.textColor = WELLGOCOLORBLACK;
    _textFiled3.delegate  = self;
    [_passViewCode addSubview:_textFiled3];
    _textFiled3.secureTextEntry = YES;
    
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = SNCOLORGRAY;
    line1.alpha           = 0.33;
    line1.frame           = CGRectMake(_textFiled1.origin.x -5, _textFiled1.origin.y + _textFiled1.height + 5, _textFiled1.width +10, 1);
    //    [_passViewCode addSubview:line1];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = SNCOLORGRAY;
    line2.alpha           = 0.33;
    line2.frame           = CGRectMake(_textFiled2.origin.x -5, _textFiled2.origin.y + _textFiled2.height + 5, _textFiled2.width +10, 1);
    [_passViewCode addSubview:line2];
    
    UIView *line3 = [[UIView alloc] init];
    line3.backgroundColor = SNCOLORGRAY;
    line3.alpha           = 0.33;
    line3.frame           = CGRectMake(_textFiled3.origin.x -5, _textFiled3.origin.y + _textFiled3.height + 5, _textFiled3.width +10, 1);
    [_passViewCode addSubview:line3];
    
    UIView *line4 = [[UIView alloc] init];
    line4.backgroundColor = SNCOLORGRAY;
    line4.alpha           = 0.33;
    line4.frame           = CGRectMake(_textFiled4.origin.x -5, _textFiled4.origin.y + _textFiled4.height + 5, _textFiled4.width +10, 1);
    [_passViewCode addSubview:line4];
    
    UIView *view5 = [[UIView alloc] init];
    view5.frame               = CGRectMake(line2.origin.x + line2.width, view1.y + 30 + 20, (screenWidth-60)/3+15, 35);
    view5.backgroundColor     = WELLGOCOLORBLACK;
    view5.layer.cornerRadius  = 4;
    view5.layer.masksToBounds = YES;
    [_passViewCode addSubview:view5];
    
    _codeButton                     = [[UIButton alloc] init];
    _codeButton.frame               = CGRectMake(line2.origin.x + line2.width, view1.y + 30 + 20, (screenWidth-60)/3+15, 35);
    _codeButton.layer.cornerRadius  = 4;
    _codeButton.layer.masksToBounds = YES;
    [_codeButton setTitleNormal:SN_Text(@"获取验证码")];
    [_codeButton.titleLabel setFont:[UIFont systemFontOfSize:11.0]];
    [_codeButton addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_codeButton setTitleColorNormal:SNCOLORWHITE];
    [_passViewCode addSubview:_codeButton];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, line4.bottom+30, HHHWIDTH, 20)];
    label.text          = SN_Text(@"收不到验证码？");
    label.textAlignment = NSTextAlignmentCenter;
//    [_passViewCode addSubview:label];
    
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, label.bottom+5, HHHWIDTH, 20)];
    textView.delegate               = self;
    textView.userInteractionEnabled = YES;
    textView.editable               = NO;
    textView.textAlignment          = NSTextAlignmentCenter;
//    [_passViewCode addSubview:textView];
    textView.text = SN_Text(@"点击这里获取帮助");
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textView.text];
    [attributedString addAttribute:NSLinkAttributeName value:@"http://note.youdao.com/noteshare?id=07f6424259fe39e764edd257c0cb4b74" range:[[attributedString string] rangeOfString:SN_Text(@"这里")]];
    textView.attributedText = attributedString;
    CGSize titleSize = [textView.text sizeWithFont:textView.font maxSize:CGSizeMake(MAXFLOAT, 20)];
    textView.frame              = CGRectMake((HHHWIDTH-titleSize.width-20)/2, label.bottom+5, titleSize.width+20, 20);
    textView.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineColorAttributeName: [UIColor lightGrayColor], NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
    // 密保 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CGFloat heightSize = FitSimpleSize(-20, 0, 8, 15, 20);
    CGFloat fontSize   = FitSimpleSize(0, 0, 1, 2, 3);
    
    _question1               = [[UILabel alloc] init];
    _question1.textColor     = SNCOLORBLACK;
    _question1.font          = SNFONT(15.0+fontSize);
    _question1.frame         = SNFRAME(20, 0, HHHWIDTH -40, 50);
    _question1.numberOfLines = 3;
    [_passViewSecret addSubview:_question1];
    
    _answer1 = [[UITextField alloc] init];
    [_answer1 changePlaceholder:SN_Text(@"请输入内容") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
    _answer1.delegate      = self;
    _answer1.textAlignment = NSTextAlignmentLeft;
    _answer1.frame         = SNFRAME(30, _question1.bottom + 8, HHHWIDTH - 60, 25);
    [_passViewSecret addSubview:_answer1];
    
    UIView *questionLine1 = [[UIView alloc] init];
    questionLine1.backgroundColor = SNCOLORGRAY;
    questionLine1.alpha           = 0.33;
    questionLine1.frame           = SNFRAME(30, _answer1.bottom + 5, _answer1.width, 1);
    [_passViewSecret addSubview:questionLine1];
    
    _question2               = [[UILabel alloc] init];
    _question2.textColor     = SNCOLORBLACK;
    _question2.font          = SNFONT(15.0+fontSize);
    _question2.frame         = SNFRAME(20, questionLine1.bottom + 10 +heightSize, HHHWIDTH -40, 50);
    _question2.numberOfLines = 3;
    [_passViewSecret addSubview:_question2];
    
    _answer2 = [[UITextField alloc] init];
    [_answer2 changePlaceholder:SN_Text(@"请输入内容") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
    _answer2.delegate      = self;
    _answer2.textAlignment = NSTextAlignmentLeft;
    _answer2.frame         = SNFRAME(30, _question2.bottom +8, HHHWIDTH - 60, 25);
    [_passViewSecret addSubview:_answer2];
    
    UIView *questionLine2 = [[UIView alloc] init];
    questionLine2.backgroundColor = SNCOLORGRAY;
    questionLine2.alpha           = 0.33;
    questionLine2.frame           = SNFRAME(30, _answer2.bottom + 5, _answer2.width, 1);
    [_passViewSecret addSubview:questionLine2];
    
    _que1 = [SERCRET1 getObjectValue];
    NSString *queString1 = @"";
    if ([_que1 isEqualToString:@"1"]) {
        queString1 = SN_Text(@"密保1");
    }
    if ([_que1 isEqualToString:@"2"]) {
        queString1 = SN_Text(@"密保2");
    }
    if ([_que1 isEqualToString:@"3"]) {
        queString1 = SN_Text(@"密保3");
    }
    _que2 = [SERCRET2 getObjectValue];
    NSString *queString2 = @"";
    if ([_que2 isEqualToString:@"4"]) {
        queString2 = SN_Text(@"密保4");
    }
    if ([_que2 isEqualToString:@"5"]) {
        queString2 = SN_Text(@"密保5");
    }
    if ([_que2 isEqualToString:@"6"]) {
        queString2 = SN_Text(@"密保6");
    }
    
    _question1.text = [NSString stringWithFormat:@"1.%@", queString1];
    _question2.text = [NSString stringWithFormat:@"2.%@", queString2];
    
    UILabel *loginLabel = [[UILabel alloc] init];
    loginLabel.font          = SNFONT(15.0+fontSize);
    loginLabel.textColor     = SNCOLORBLACK;
    loginLabel.textAlignment = NSTextAlignmentLeft;
    loginLabel.frame         = SNFRAME(20, questionLine2.bottom + 30 +heightSize, HHHWIDTH - 40, 20);
    loginLabel.text          = SN_Text(@"新密码:");
    [_passViewSecret addSubview:loginLabel];
    
    _passWord1 = [[UITextField alloc] init];
    _passWord1.secureTextEntry = YES;
    [_passWord1 changePlaceholder:SN_Text(@"请输入新的密码") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
    _passWord1.delegate      = self;
    _passWord1.textAlignment = NSTextAlignmentLeft;
    _passWord1.frame         = SNFRAME(30, loginLabel.bottom + 20, HHHWIDTH - 60, 25);
    [_passViewSecret addSubview:_passWord1];
    
    UIView *passWordLine1 = [[UIView alloc] init];
    passWordLine1.backgroundColor = SNCOLORGRAY;
    passWordLine1.alpha           = 0.33;
    passWordLine1.frame           = SNFRAME(30, _passWord1.bottom + 5, _passWord1.width, 1);
    [_passViewSecret addSubview:passWordLine1];
    
    _passWord2 = [[UITextField alloc] init];
    _passWord2.secureTextEntry = YES;
    [_passWord2 changePlaceholder:SN_Text(@"请确认新的密码") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
    _passWord2.delegate      = self;
    _passWord2.textAlignment = NSTextAlignmentLeft;
    _passWord2.frame         = SNFRAME(30, passWordLine1.bottom + 25 +heightSize, HHHWIDTH - 60, 25);
    [_passViewSecret addSubview:_passWord2];
    
    UIView *passWordLine2 = [[UIView alloc] init];
    passWordLine2.backgroundColor = SNCOLORGRAY;
    passWordLine2.alpha           = 0.33;
    passWordLine2.frame           = SNFRAME(30, _passWord2.bottom + 5, _passWord2.width, 1);
    [_passViewSecret addSubview:passWordLine2];
    
    UIButton *nextButton = [[UIButton alloc] init];
    nextButton.frame = SNFRAME(30, _passViewSecret.bottom - 200, HHHWIDTH - 60, 35);
    [nextButton setTitle:SN_Text(@"找回密码") forState:UIControlStateNormal];
    nextButton.backgroundColor = SNCOLORBLACK;
    [nextButton setTitleColor:SNCOLORWHITE forState:UIControlStateNormal];
    [nextButton setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    nextButton.layer.cornerRadius = 5;
    [nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [nextButton setFontSize:15.0];
    [_passViewSecret addSubview:nextButton];
}

- (void)nextButtonClick {
    if (_answer1.text.length <= 0 || _answer2.text.length <= 0 || _passWord1.text.length <= 0 || _passWord2.text.length <= 0) {
        SHOWMBProgressHUD(nil, SN_Text(@"信息请填写完整"), nil, NO, 2.0);
        return;
    }
    
    if (![_passWord1.text isPassword] || ![_passWord2.text isPassword]) {
       SHOWMBProgressHUD(nil,SN_Text( @"密码必须为6～14位的数字或字母"),nil, NO, 2.0);
        return;
    }
    
    if (![_passWord1.text isEqualToString:_passWord2.text]) {
        SHOWMBProgressHUD(nil, SN_Text(@"您输入的两个密码不匹配。"), nil, NO, 2);
        return;
    }
    
    if (_answer1.text.length <= 0 || _answer2.text.length <= 0) {
        SHOWMBProgressHUD(nil,SN_Text(@"请填写问题答案"), nil, NO, 2.0);
        return;
    }
     SHOWMBProgressHUDIndeterminate(SN_Text(@"请等待..."), nil, @"登录中");
    NSDictionary *dic1 = @{
                           @"question":_que1, @"answer":_answer1.text,
                           };
    NSDictionary *dic2 = @{
                           @"question":_que2, @"answer":_answer2.text,
                           };
    NSArray *result = [NSArray arrayWithObjects:dic1, dic2, nil];
    [SERCRETPASSWORD setObjectValue:_passWord2.text];
    [LOGINCODE setObjectValue:result];
    [SN_Net setEmailSecretsuccess:^(id object) {
          [self delayRetLoading];
        if (object[@"ret"] && [object[@"ret"] intValue] == 0) {
            NSDictionary *dic = [object objectForKey:@"data"];
            NSLog(@"dic>>>>>>>%@", dic);
            NSInteger is_success = [dic[@"is_success"] integerValue];
            if (is_success == 1) {
                SHOWMBProgressHUD(nil,SN_Text(@"密码修改成功"), nil, NO, 2.0);
                [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(passWordSuccess)
                                                           object:nil];
                [self performSelector:@selector(passWordSuccess) withObject:nil afterDelay:2.0];
            }
            else
            {
                SHOWMBProgressHUD(nil,SN_Text(@"密保错误"), nil, NO, 2.0);
            }
        }
    } fail:^(id object) {
        [self delayRetLoading];
    }];
    [self performSelector:@selector(delayRetLoading) withObject:nil afterDelay:30.0];
}

- (void)passWordSuccess
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRetLoading) object:nil];
    [kK_App enterLoginVc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)buttonClickSender:(UIButton *)sender {
    if (_passWordArray.count == 1) {
        return;
    }
    SHESelectTable *sTable;
    sTable                = [[SHESelectTable alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenWidth) withTableFrame:CGRectMake(_findLabelType.left, _findLabelType.bottom, _findLabelType.width*0.95, 40*_passWordArray.count) withDataArray:_passWordArray withSelectindex:_passWordType];
    sTable.selTable.frame = CGRectMake(_findLabelType.left-3, _findLabelType.bottom +40*_passWordArray.count -10, _findLabelType.width *0.95, 40*_passWordArray.count);
    sTable.returnBlock    = ^(NSString *cityString)
    {
        if ([cityString isEqualToString:SN_Text(@"通过密保问题")]) {
            _passWordType = 1;
        } else {
            _passWordType = 2;
        }
        _findLabelType.text = cityString;
        [self changeFindPassWord];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:sTable];
    [UIView animateWithDuration:0.2 animations:^{
        sTable.selTable.alpha = 1;
    }];
}

- (void)changeFindPassWord {
    _passViewSecret.hidden = YES;
    _passViewCode.hidden   = YES;
    if (_passWordArray.count == 1) {
        _passViewCode.hidden = NO;
    } else {
        if (_passWordType == 1) {
            _passViewSecret.hidden = NO;
            _answer1.text          = @"";
            _answer2.text          = @"";
            _passWord1.text        = @"";
            _passWord2.text        = @"";
            CGFloat fontSize = FitSimpleSize(0, 0, 1, 2, 3);
            [_answer1 changePlaceholder:SN_Text(@"请输入内容") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
            [_answer2 changePlaceholder:SN_Text(@"请输入内容") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
            [_passWord1 changePlaceholder:SN_Text(@"请输入新的密码") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
            [_passWord2 changePlaceholder:SN_Text(@"请确认新的密码") withColor:HHHFONTCOLOR withFont:14.0+fontSize];
        } else {
            _passViewCode.hidden = NO;
            _textFiled2.text     = @"";
            _textFiled3.text     = @"";
            _textFiled4.text     = @"";
            [_textFiled2 changePlaceholder:SN_Text(@"请输入验证码") withColor:HHHFONTCOLOR withFont:14.0];
            [_textFiled3 changePlaceholder:SN_Text(@"请输入密码") withColor:HHHFONTCOLOR withFont:14.0];
            [_textFiled4 changePlaceholder:SN_Text(@"请再次输入密码") withColor:HHHFONTCOLOR withFont:14.0];
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"http://note.youdao.com/noteshare?id=07f6424259fe39e764edd257c0cb4b74"]) {
        [[UIApplication sharedApplication] openURL:URL];
        return NO;
    }
    return YES;
}

// 文本编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (_showInfo == NO) {
        return;
    }
    
    if (textField == _textFiled3) {
        if ([_textFiled3.text isPassword] && _textFiled3.text.length > 0) {
        } else {
            SHOWMBProgressHUD(HHH_Text(@"密码必须为6～14位的数字或字母"), nil, nil, NO, 2.0f);
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
}

- (void)codeButtonClick:(UIButton *)sender {
    if (_codeCount == CODECOUNTVALUE) {
        if (KK_ShareData.registrationType == 2) {
            [self getCodeWithEmailClick];   // 邮箱验证码
        }
    } else {
        return;
    }
    [_codeButton setTitleColorNormal:HHHFONTCOLOR];
    [self performSelector:@selector(delayCodeButtonReset) withObject:nil afterDelay:0.1];
    NSLog(@"codeButtonClick>>>>>");
}

// 获取邮箱的验证码
- (void)getCodeWithEmailClick {
    NSString *str = [NSString stringWithFormat:@"%@: %@", HHH_Text(@"我们将向这封电子邮件发送验证码"), [SERCRETEMAIL getObjectValue]];
    
    NSLog(@"str>>>>%@", str);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:HHH_Text(@"确认电子邮件")
                                                    message:str delegate:self
                                          cancelButtonTitle:HHH_Text(@"取消")
                                          otherButtonTitles:HHH_Text(@"确定"), nil];
    alert.tag = 100;
    [alert show];
}

- (BOOL)isValidateEmail:(NSString *)email {
    NSString    *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:email];
}

- (void)countDown {
    if (_codeCount > 0) {
        _codeCount--;
        [self performSelector:@selector(countDown) withObject:nil afterDelay:1.0];
        NSString *codeS = [NSString stringWithFormat:@"%ld", (long)_codeCount];
        [_codeButton setTitleNormal:codeS];
    }
    
    if (_codeCount == 0) {
        _codeCount = CODECOUNTVALUE;
        [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
    }
}

- (void)delayCodeButtonReset {
    [_codeButton setTitleColorNormal:WELLGOCOLORWHITE];
}

// 注册按钮
- (void)loginButtonClick:(UIButton *)sender {
    [_loginButton setTitleColorNormal:HHHFONTCOLOR];
    [self performSelector:@selector(delayLoginButtonReset) withObject:nil afterDelay:0.1];
    NSLog(@"注册ButtonClick>>>>>");
    if (KK_ShareData.registrationType == 2) {
        [self setPassWordWithEmail];
    }
    // 成功之后进入用户信息
}

- (void)setPassWordWithEmail {
    {
        if (_textFiled2.text.length <= 0 || _textFiled3.text.length <= 0) {
            SHOWMBProgressHUD(HHH_Text(@"信息请填写完整"), nil, nil, NO, 2.0);
            return;
        }
        if (![_textFiled3.text isPassword]) {
            SHOWMBProgressHUD(HHH_Text(@"密码必须为6～14位的数字或字母"), nil, nil, NO, 2.0);
            return;
        }
        if (![_textFiled4.text isPassword]) {
            SHOWMBProgressHUD(HHH_Text(@"密码必须为6～14位的数字或字母"), nil, nil, NO, 2.0);
            return;
        }
        if (![_textFiled4.text isEqualToString:_textFiled3.text]) {
            SHOWMBProgressHUD(HHH_Text(@"您输入的两个密码不匹配。"),nil, nil, NO, 2);
            return;
        }
        SHOWMBProgressHUDIndeterminate(SN_Text(@"请等待..."), nil, @"登录中");
        NSString     *passWordMd5 = [self md5:_textFiled3.text];
        NSDictionary *dict        = @{
                                      @"email":[SERCRETEMAIL getObjectValue], @"password":passWordMd5, @"code":_textFiled2.text, @"code":_textFiled2.text,@"app_id":SN_Net.app_id
                                      };
        // 开始注册
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *url = @"/user/resetpwd";
        
        [manager POST:url parameters:dict success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject)
         {
             [self delayRetLoading];
             if (responseObject[@"ret"] && [responseObject[@"ret"] intValue] == 0) {
                 SHOWMBProgressHUD(nil,SN_Text(@"密码修改成功"), nil, NO, 3.0);
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(passWordSuccess)
                                                            object:nil];
                 [self performSelector:@selector(passWordSuccess) withObject:nil afterDelay:3.0];
             } else {
                 SHOWMBProgressHUD(HHH_Text(responseObject[@"message"]), nil, nil, NO, 2.0);
             }
         }    failure  :^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
            [self delayRetLoading];
             SHOWMBProgressHUD(HHH_Text([error localizedDescription]),nil, nil, NO, 2.0);
         }];
    }
    [self performSelector:@selector(delayRetLoading) withObject:nil afterDelay:30.0];
}

- (void)delayRetLoading
{
    HIDDENMBProgressHUD;
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRetLoading) object:nil];
}

- (void)dealloc
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRetLoading) object:nil];
}

//MD5加密
- (NSString *)md5:(NSString *)input {
    const char    *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, strlen(cStr), digest);   // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

- (void)delayLoginButtonReset {
    [_loginButton setTitleColorNormal:WELLGOCOLORWHITE];
}

- (void)backClick {
    _showInfo = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        if (1 == buttonIndex) {
            NSDictionary *dict = @{ @"phone":[SERCRETEMAIL getObjectValue], @"type":@(2), @"country_code":_areaButton.titleLabel.text,@"app_id":SN_Net.app_id};
            // 开始注册
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            NSString *url = @"/user/captcha";
            [manager GET:url parameters:dict success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject)
             {
                 // 注册成功跳转回去
                 if (responseObject[@"ret"] && [responseObject[@"ret"] intValue] == 0) {
                     SHOWMBProgressHUD(HHH_Text(@"请求验证代码成功"), nil, nil, NO, 2.0);
                 } else {
                     SHOWMBProgressHUD(HHH_Text(responseObject[@"message"]), nil, nil, NO, 2.0);
                 }
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
             }   failure   :^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
                 NSLog(@"22222 %@", HHH_Text([error localizedDescription]) );
                 SHOWMBProgressHUD(HHH_Text([error localizedDescription]),nil, nil, NO, 2.0);
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
             }];
        } else {
            NSLog(@"发送验证码取消按钮>>>>");
            _codeCount = CODECOUNTVALUE;
            [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
        }
    }
    if (alertView.tag == 100) {
        if (1 == buttonIndex) {
            [self countDown];
        } else {
            NSLog(@"发送验证码取消按钮>>>>");
        }
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 100) {
        if (buttonIndex == 1) {
            NSString *language = [NSString string];
            if ([[ShareData sharedInstance].language hasPrefix:@"zh-Hans"]) {
                language = @"cmn-Hans";
            } else {
                language = @"en";
            }
            // 邮件验证码
            NSDictionary *dict = @{ @"email":[SERCRETEMAIL getObjectValue], @"type":@(2), @"language":language,@"app_id":SN_Net.app_id};
            // 开始注册
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            NSString *url = @"/user/captcha";
            [manager GET:url parameters:dict success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject)
             {
                 // 注册成功跳转回去
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
                 if (responseObject[@"ret"] && [responseObject[@"ret"] intValue] == 0) {
                     SHOWMBProgressHUD(HHH_Text(@"请求验证代码成功"), nil, nil, NO, 2.0);
                 } else {
                     SHOWMBProgressHUD(HHH_Text(responseObject[@"message"]), nil, nil, NO, 2.0);
                 }
             }   failure   :^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
                 _codeCount = CODECOUNTVALUE;
                 [_codeButton setTitleNormal:HHH_Text(@"获取验证码")];
                 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(countDown) object:nil];
                 SHOWMBProgressHUD(HHH_Text([error localizedDescription]),nil, nil, NO, 2.0);
             }];
        }
    }
}

@end

